jQuery(document).ready(function($) {

	$('#startPost').click(function(){
		var practiceTestID = $('.radioChange').attr('id').split("#");
		var izUserID = practiceTestID[0];
		var izQuizID = practiceTestID[1];
		var timerStart = $(this).attr('class').split("-");
		$('.startPracticeTest').html('<img src="'+core_js_script.ajax_image+'load.gif" />').fadeIn();
		$.post(ajax_script.ajaxurl,{
            action: "startPracticeTest",
            izQuizID:izQuizID,
            izUserID:izUserID,
            timer:timerStart[1]
        },function(startPost){
            if(startPost.success == true){
            	$('.startPracticeTest').fadeOut();
            	window.location.reload();
            }else{
            	$('.startPracticeTest').fadeOut();
            	alert("Không thể bắt đầu - có lỗi sảy ra");
            	return false;
            }
        });
		return false;
	});

	$('.radioChange input:radio').screwDefaultButtons({ 
		image: "url("+theme_js_script.ajax_image+"radio.png)",
		width:	 20,
		height:	 20
	});


	$('.quizTime').countdown(core_js_script.timerDoing, function(event) {
		$(this).html(event.strftime('Bạn còn %M:%Ss'));
	});

	$('.izQuizQuestion').hide();
	$('.izQuizQuestion:first').show();
	$(document).on('click','.izApplyAnswer',function(){
		var _this = $(this);
		var currentQuestion = _this.parent().attr('data-current');
		var total = parseInt($('.quizNumner > span.total').text());
		var practiceTestID = $('.radioChange').attr('id').split("#");
		var izUserID = practiceTestID[0];
		var izQuizID = practiceTestID[1];
		var answerID = _this.find('input').attr('id').split("_");
		var percentDoing = parseInt(currentQuestion)/total*100;
		var isYesNoQuestion = (_this.find('input').attr('data-question')=="YesNo") ? true : false;
		var valAnswer = isYesNoQuestion == true ? _this.find('input').attr('value') : "";
		var nextQuestion = _this.parent().attr('data-next');
		var hideQuestion = $("#question-"+currentQuestion);
		var showQuestion = $("#question-"+nextQuestion);
		$('.quizer').css('opacity','0.5');
		$('.loading').fadeIn();
		$.post(ajax_script.ajaxurl,{
            action: "updatePracticeTestTemporaryResult",
            izQuizID:izQuizID,
            izUserID:izUserID,
            currentQuestion:currentQuestion,
            izAnswer:answerID[1],
            izQuestionID:answerID[2],
            percentDoing:percentDoing,
            total:total,
            valAnswer:valAnswer
        },function(update){
            if(update.success == true){
            	$('.loading').fadeOut();
            	$('.quizer').css('opacity','1');
               	hideQuestion.slideUp();
               	if(nextQuestion=="last"){
               		$('.izQuizInfo').slideUp(500,function(){
               			window.location.reload();
               		});
               	}else{
					showQuestion.slideDown();
					$('.quizNumner > span.current').text(nextQuestion);
				}
            }
        });
	});
});

function updateProgressBar(currentPercent,percentQuestion,callback)
{
	if(currentPercent<=percentQuestion){
		jQuery('.progress-bar').attr('aria-valuetransitiongoal',currentPercent).progressbar();
	}
	callback();
}
